package org.gc.amino.ia;

import java.awt.geom.Point2D;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;

public class Scheduler {
	private Queue<Action> action_queue;
	
	static public class Action {
		private Point2D.Float direction;
		
		/**
		 * Time delay relative to previous action
		 */
		private int time_delay;
		
		public Action(Point2D.Float direction, int time_delay) {
			this.direction = direction;
			this.time_delay = time_delay;
		}

		public Point2D.Float getDirection() {
			return direction;
		}

		public int getTimeDelay() {
			return time_delay;
		}
		
		public Point2D.Float getNextMove() {
			this.time_delay --;
			
			Point2D.Float direction = null;
			
			if(this.isDone()) {
				direction = this.direction;
			}
			
			return direction;
		}
		
		public boolean isDone() {
			return (this.time_delay <= 0);
		}
		
	}
	
	public Scheduler() {
		this.action_queue = new LinkedList<Action>();
	}
	
	Point2D.Float getNextDirection() {
		Point2D.Float direction = null;
		
		if(!this.action_queue.isEmpty()) {
			Action action = this.action_queue.peek();
			
			// Once the action is done, it is removed from the queue
			direction = action.getDirection();
			if(action.isDone()) {
				this.action_queue.remove();
			}
		}
		
		return direction;
	}
	
	public void add(Point2D.Float direction, int time_delay) {
		this.action_queue.add(new Action(direction, time_delay));
	}
	
	public boolean isEmpty() {
		return this.action_queue.isEmpty();
	}
	
}
